#include "list.h"
#include <stdlib.h>
#include "fatal.h"

/* Place in the interface file */

int Equal(ElementType x, ElementType y)
{
    return ((x.a == y.a) && (x.b == y.b));
}
void Print(ElementType x)
{
    printf("(%d, %d)", x.a,x.b);
}
/*
int Equal(ElementType x, ElementType y)
{
	return x.orderid == y.orderid;
}

void Print(ElementType order)
{
	printf("orderid: %04d, stockid:%04d, price: %6.1f, quantity: %4d, b/s: %c\n",
			order.orderid, order.stockid, order.price, order.quantity, order.bs);
	return;
}*/
struct Node
{
    ElementType Element;
    Position    Next;
};



List
CreateList( void )
{
    List L;

    L = (List)malloc( sizeof( struct Node ) );
    if( L == NULL )
        Error( "Out of memory!" );
    L->Next = NULL;
    return L;
}

void
MakeEmpty( List L )
/*
		{
            if( L == NULL )
                Error( "Must use CreateList first" );
            else
				DeleteList( L );
		}
*/
{
    Position P, Tmp;

    /* 1*/      P = L->Next;  /* Header assumed */
    /* 2*/      L->Next = NULL;
    /* 3*/      while( P != NULL )
    {
        /* 4*/          Tmp = P->Next;
        /* 5*/          free( P );
        /* 6*/          P = Tmp;
    }
}


/* START: fig3_8.txt */
/* Return true if L is empty */

int
IsEmpty( List L )
{
    return L->Next == NULL;
}
/* END */

/* START: fig3_9.txt */
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */

int IsLast( Position P, List L )
{
    return P->Next == NULL;
}
/* END */

/* START: fig3_10.txt */
/* Return Position of X in L; NULL if not found */

Position
Find( ElementType X, List L )
{
    Position P;

    /* 1*/      P = L->Next;
    /* 2*/      while( P != NULL && !Equal(P->Element, X))
        /* 3*/
        P = P->Next;

    /* 4*/      return P;
}
/* END */
Position Last( List L )
{
    Position P;

    /* 1*/      P = L;
    /* 2*/      while( P->Next != NULL )
        /* 3*/
        P = P->Next;

    /* 4*/      return P;
}

/* START: fig3_11.txt */
/* Delete (after legal position P) */
/* Assume use of a header node */
void
Delete(List L, Position P )
{
    Position TmpCell;

    TmpCell = P->Next;
    P->Next = TmpCell->Next;  /* Bypass deleted cell */
    free( TmpCell );
}

/* END */

/* START: fig3_12.txt */
/* If X is not found, then Next field of returned value is NULL */
/* Assumes a header */

Position
FindPrevious( ElementType X, List L )
{
    Position P;

    /* 1*/      P = L;
    /* 2*/      while( P->Next != NULL && !Equal(P->Next->Element, X) )
        /* 3*/
        P = P->Next;

    /* 4*/      return P;
}
/* END */

/* START: fig3_13.txt */
/* Insert (after legal position P) */
/* Header implementation assumed */
/* Parameter L is unused in this implementation */

void
Insert( ElementType X, List L, Position P )
{
    Position TmpCell;

    /* 1*/      TmpCell = (Position)malloc( sizeof( struct Node ) );
    /* 2*/      if( TmpCell == NULL )
        /* 3*/
        Error( "Out of space!!!" );

    /* 4*/      TmpCell->Element = X;
    /* 5*/      TmpCell->Next = P->Next;
    /* 6*/      P->Next = TmpCell;
}
/* END */

#if 0
/* START: fig3_14.txt */
/* Incorrect DeleteList algorithm */

void
DeleteList( List L )
{
    Position P;

    /* 1*/      P = L->Next;  /* Header assumed */
    /* 2*/      L->Next = NULL;
    /* 3*/      while( P != NULL )
    {
        /* 4*/          free( P );
        /* 5*/          P = P->Next;
    }
}
/* END */
#endif

/* START: fig3_15.txt */
/* Correct DeleteList algorithm */

void
DeleteList( List L )
{
    PtrToNode P, Tmp;

    P = L;
    while( P != NULL )
    {
        Tmp = P->Next;
        free( P );
        P = Tmp;
    }
}
/* END */

Position
Header( List L )
{
    return L;
}

Position
First( List L )
{
    return L->Next;
}

Position
Advance( Position P )
{
    return P->Next;
}

ElementType
Retrieve( Position P )
{
    return P->Element;
}

void Modify( ElementType X, Position P )
{
    P->Element = X;
}

void PrintList(List L)
{
    Position pL;
    ElementType e;

    pL = First(L);
    while (pL)
    {
        e = Retrieve(pL);
        Print(e);
        pL = Advance(pL);
    }
    printf("\n");
}
